<?php
/**
 * @author: trongkhoi
 */
$NoPhotoUrl = Yii::app()->controller->assetDir . "/img/no-image.png";
?>
<div class="box span12">
    <div class="box-header">
        <h2>
            <i class="icon-cogs"></i>
            <?= Yii::t('user', 'Config') ?> <?php echo $this->title ?>
        </h2>        
    </div>
    <div class="box-content">
        <?php
        $formOptions = array(
            'id' => 'option-form',
            'enableClientValidation' => true,
//            'enableAjaxValidation' => true,
            'htmlOptions' => array(
                'class' => 'form-horizontal',
                'enctype' => 'multipart/form-data',
            ),
            'clientOptions' => array(
                'validateOnSubmit' => true,
            ),
        );

        $form = $this->beginWidget('CActiveForm', $formOptions);
        ?>
        <fieldset>
            <?php foreach ($options as $option): ?>
                <?php
                $help = "";
                $preview = "";
                $link = "";
                $input = "";
                /**
                 * ID của phần Input, phục vụ cho việc áp dụng các sự kiện js cho nó
                 */
                $inputID = $option->id;
                // Label lấy từ trường description
                $label = CHtml::label($option->description, $inputID, array(
                        'class' => 'control-label'
                ));
                $value = $option->value;

                $htmlOptions = array(
                    'id' => $inputID,
                    'name' => "Option[$inputID]",
                    'class' => "span10",
                );
                ?>
            <div class="control-group">
                    <?php echo $label; ?>
                    <div class ="controls">
                <?php switch ($option->data_type)
                /**
                 *  Nếu có truyền vào loại Input thì dùng loại đó nếu không thì 
                 *  dùng textField
                 */
                {
                    case "editor":
//                        $editor_ids[] = $inputID;
//                        $fieldType = 'textArea';
//                        break;
                    $this->widget('ext.ckeditor.CKEditor', array(
                                    'model' => $option,
                                    'attribute' => $inputID,
                                    'editorTemplate' => 'full', // Toolbar settings (full, basic, advanced)
                                ));
                        break;
                    case "image":
                        /**
                         * Hiển thị ảnh được chọn
                         */
                        $imgSrc = $value != '' ? Yii::app()->baseUrl . "/$value" : $NoPhotoUrl;
                        $imgOption = array(
                            'id' => "prev_$inputID",
                            'style' => 'max-height:150px;',
                            'class' => 'tl',
                            'rel' => 'post',
                        );
                        echo CHtml::image($imgSrc, $help, $imgOption) . "<br/>";
                        echo "<input type='file' name='Option[$inputID]'/>";
                        break;
//                   echo CHtml::image($imgSrc, $help, $imgOption);
//                        $this->widget('ext.ckeditor.EImageFinder', array('field' => $inputID,'field_name'=>"Option[$inputID]"));
//                        break;

                    case "textArea":
                        echo $form->textArea($option, "value", $htmlOptions);
                        break;
                    case "number":
                        $htmlOptions['class'].=' number_input';
                        echo $form->textField($option, "value", $htmlOptions);
                        break;
                    case "alpha":
                        $htmlOptions['class'].=' alpha_input';
                        echo $form->textField($option, "value", $htmlOptions);
                        break;
                    case "date":
                        $htmlOptions['class'].=' datepicker';
                        echo $form->textField($option, "value", $htmlOptions);
                        break;
                    case "alphanumeric":
                        $htmlOptions['class'].=' alphanumeric';
                        echo $form->textField($option, "value", $htmlOptions);
                        break;
                    case "tags":
                        $htmlOptions['class'].=' tags_input';
                        echo $form->textField($option, "value", $htmlOptions);
                        break;
                    default:
                        echo $form->textField($option, "value", $htmlOptions);
                        break;
                }

//                if ($input == '')
//                {
//                    $input = $form->{$fieldType}($option, "value", $htmlOptions);
//                }
                ?>

                <!--<div class="control-group">-->
                    <?php // echo $label; ?>
                    <!--<div class ="controls">-->
                        <?php // echo $preview; ?>
                        <?php // echo $input; ?>
                        <?php // echo $link; ?>
                    </div>
                </div>

            <?php endforeach; ?>    
<!--            
            <div class="control-group">
                <label class="control-label">Content file robots.txt</label>
                <div class ="controls">
                    <?php
//                    $content = '';
//                    $file = 'robots.txt';
//                    if (file_exists($file))
//                    {
//                        $content = file_get_contents($file);
//                    }
                    ?>
                    <?php // echo CHtml::textArea('robots', $content, array('class' => 'input-xlarge span12', 'rows' => 12)) ?>
                </div>
            </div>-->
            <div class="control-group">       
                <div class ="controls">
                    <button class="btn btn-primary"><?= Yii::t('user','Save')?></button>
                    <?php
//                    echo CHtml::ajaxSubmitButton("Save AJAX", '', array(
//                        'success' => "function(data) {formSuccess(data);}",
//                        ), array(
//                        'id' => 'submitItem',
//                        'name' => 'submitItem',
//                        'class' => 'btn btn-primary'
//                    ));
                    ?>                    
                </div>
            </div>
        </fieldset>
        <?php $this->endWidget(); ?>
    </div>
</div>